﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ESupervision.Models;
using Microsoft.AspNet.Identity;


namespace ESupervision.Controllers
{
    public class ForumTopicController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: /ForumTopic/
        public async Task<ActionResult> Index()
        {
            var forumtopics = db.ForumTopics.Include(f => f.ForumTopicPoster);
            return View(await forumtopics.ToListAsync());
        }

        // GET: /ForumTopic/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumTopic forumtopic = await db.ForumTopics.FindAsync(id);
            if (forumtopic == null)
            {
                return HttpNotFound();
            }
            forumtopic.ViewCount++;
            db.SaveChanges();
            ViewBag.ForumTopic = forumtopic;

            String userId = User.Identity.GetUserId();

            ForumReply forumreplay = new ForumReply
            {
                ForumReplyPosterID = userId,
                ForumTopicID = forumtopic.ForumTopicID
            };

            return View(forumreplay);
        }

        // GET: /ForumTopic/Create
        public ActionResult Create()
        {
            //ViewBag.ForumTopicPosterID = new SelectList(db.Students, "Id", "UserName");
            string userId = User.Identity.GetUserId();

            ForumTopic forumtopic = new ForumTopic
            {
                ForumTopicPosterID = userId,
                ForumTopicPostedTime = DateTime.Now
            };
            return View(forumtopic);
        }

        // POST: /ForumTopic/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ForumTopicID,ForumTopicTitle,ForumTopicContent,ForumTopicPostedTime,ForumTopicPosterID")] ForumTopic forumtopic)
        {
            if (ModelState.IsValid)
            {
                forumtopic.ForumTopicPostedTime = DateTime.Now;
                db.ForumTopics.Add(forumtopic);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");

            }

            // ViewBag.ForumTopicPosterID = new SelectList(db.Students, "Id", "UserName", forumtopic.ForumTopicPosterID);
            return View(forumtopic);
        }

        // GET: /ForumTopic/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumTopic forumtopic = await db.ForumTopics.FindAsync(id);
            if (forumtopic == null)
            {
                return HttpNotFound();
            }
            ViewBag.ForumTopicPosterID = new SelectList(db.Students, "Id", "UserName", forumtopic.ForumTopicPosterID);
            return View(forumtopic);
        }

        // POST: /ForumTopic/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ForumTopicID,ForumTopicTitle,ForumTopicContent,ForumTopicPostedTime,ForumTopicPosterID")] ForumTopic forumtopic)
        {
            if (ModelState.IsValid)
            {
                db.Entry(forumtopic).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ForumTopicPosterID = new SelectList(db.Students, "Id", "UserName", forumtopic.ForumTopicPosterID);
            return View(forumtopic);
        }

        // GET: /ForumTopic/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumTopic forumtopic = await db.ForumTopics.FindAsync(id);
            if (forumtopic == null)
            {
                return HttpNotFound();
            }
            return View(forumtopic);
        }

        // POST: /ForumTopic/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            ForumTopic forumtopic = await db.ForumTopics.FindAsync(id);
            db.ForumTopics.Remove(forumtopic);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateReply([Bind(Include = "ForumReplyID,ForumReplyContent,ForumReplyPostedTime,ForumTopicID,ForumReplyPosterID")] ForumReply forumreply)
        {
            ForumTopic forumtopic = await db.ForumTopics.FindAsync(forumreply.ForumTopicID);

            if (forumtopic == null)
            {
                return HttpNotFound();
            }

            ViewBag.ForumTopic = forumtopic;

            if (ModelState.IsValid)
            {
                forumreply.ForumReplyPostedTime = DateTime.Now;
                db.ForumReplies.Add(forumreply);
                await db.SaveChangesAsync();
            }
            return RedirectToAction("Details/" + forumreply.ForumTopicID);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
